
************************************************************************************************************************;
********************************************* Baseler program code Stata ***********************************************;
************************************************************************************************************************;

clear all

*** Set version
version 15

*** Control monitor output
set more off
set logtype text
set linesize 255

*** Set date (for unique file naming)
local date 071724

*** Set path of ado
sysdir set PERSONAL "E:\fdz\00_tagesgeschaeft\stata_ado"
mata mata mlib index

*** Macros for paths
global datapath "E:\fdz\mikrozensus\03_on_site_files\files_stata\"
global outputpath "E:\fdz\mikrozensus\04_laufende_projekte\uni_rochester\4093-2020_alessandria\output\output_20240324\"

*** Macros for names of datasets and output
global dataname_2005 mz_2005.dta
global dataname_2000 mz_2000.dta
global dataname_1991 mz_1991.dta
global dataname_1989 mz_1989.dta
global dataname_1985 mz_1985.dta
global outputname Baseler_output_micro_`date'


********************************************************************************************************************
*** I. Data processing
********************************************************************************************************************


*** a. Open and append all datasets
*** Direct selection of necessary variables (memory space- and runtime-efficient).

use EF1 EF3S-EF5S EF12 EF25 EF30 EF31 EF46 EF47 EF49 EF57 EF84 EF119 EF309-EF312 EF366 EF367-EF370 EF372-EF375 EF377-EF379 EF436 EF570 EF952 EF2001 ///
using "$datapath\$dataname_2005", clear
rename EF* mc05_EF*
generate year = 2005
compress

append using "$datapath\$dataname_2000", ///
keep(EF1 EF3s-EF5s EF28 EF32 EF33 EF35 EF43 EF44 EF45 EF53-EF54 EF56-EF58 EF95 EF128 EF286-EF289 EF372 EF505 EF506 EF567 EF568 EF712 EF750)
rename EF* mc00_EF*
replace year = 2000 if year==.
compress

append using "$datapath\$dataname_1991", ///
keep(EF1 EF3S-EF5S EF6 EF26 EF27 EF35 EF37 EF38 EF41 EF47-EF48 EF50-EF53 EF61 EF93 EF121 EF122 EF146 EF253 EF59 EF307)
replace year = 1991 if year==.

append using "$datapath\$dataname_1989", ///
keep(EF1 EF3s-EF5s EF6 EF26 EF27 EF35 EF37 EF38 EF41 EF47-EF48 EF50-EF53 EF61 EF93 EF121 EF122 EF146 EF253)
replace year = 1989 if year==.

append using "$datapath\$dataname_1985", ///
keep(EF1 EF3s-EF5s EF6 EF26 EF27 EF35 EF37 EF38 EF41 EF47-EF48 EF50-EF53 EF61 EF93 EF121 EF122 EF146 EF253)
replace year = 1985 if year==.
rename EF* mc85_91_EF*
rename mc85_91_EF307 mc91_EF307 //this var only appears in 1991
rename mc85_91_EF59 mc91_EF59 //this var only appears in 1991
compress

*** b0. Education recode to ISCED-1997 from https://www.gesis.org/missy/files/documents/MZ/isced/mz05_isced_gml.do

**2005
recode mc05_EF310 0 = 0 1 = 4 2 3 = 5 4 5 = 6 9 = 3, generate (as)
replace as = 1 if (mc05_EF309 == 8)

recode mc05_EF312 0 = 0 1 2 = 4 3 4 = 5 5 6 7 = 6 8 9 = 7 10 = 8 99 = 3, generate (ba)
replace ba = 1 if mc05_EF311 == 8

generate is = 99
replace is = 1 if (as == 1 & ba == 1)
replace is = 3 if ((as == 0 | as == 1) & (ba == 3 | ba == 4)) | ((as == 3 | as == 4) & (ba == 0 | ba == 1 | ba == 3 | ba == 4))
replace is = 4 if (as == 5 & (ba == 1 | ba == 3 | ba == 4))
replace is = 5 if ((as == 0 | as == 1 | as == 3 | as == 4 | as == 5) & ba == 5)
replace is = 6 if (as == 6 & (ba == 1 | ba == 3 | ba == 4))
replace is = 7 if (as == 6 & ba == 5)
replace is = 8 if (ba == 6)
replace is = 9 if (ba == 7)
replace is = 10 if (ba == 8)

rename is mc05_is
drop as ba

**2000
recode mc00_EF287 0=0 1=4 2 3=5 4 5=6 9=3, generate(as)
replace as=1 if (mc00_EF286==8)
replace as=2 if (mc00_EF286==9)

recode mc00_EF289 0=0 1 2=4 3 4=5 5 6=6 7 8=7 9=8 99=3, generate(ba)
replace ba=1 if mc00_EF288==8
replace ba=2 if mc00_EF288==9

generate is=99
replace is=1 if (as==1 & (ba==1 | ba==2)) | (as==2 & ba==1)
replace is=3 if ((as==1 | as==2) & (ba==3 | ba==4)) | ((as==3 | as==4) & (ba==0 | ba==1 | ba==2 | ba==3 | ba==4))
replace is=4 if (as==5 & (ba==1 | ba==2 | ba==3 | ba==4))
replace is=5 if ((as==1 | as==2 | as==3 | as==4 | as==5) & ba==5)
replace is=6 if (as==6 & (ba==1 | ba==2 | ba==3 | ba==4))
replace is=7 if (as==6 & ba==5)
replace is=8 if (ba==6)
replace is=9 if (ba==7)
replace is=10 if (ba==8)

rename is mc00_is
drop as ba

**1991
recode mc85_91_EF121 1=4 2 3=5 4 5=6 8=3, generate(as)
replace as=0 if (mc91_EF59==8 | mc91_EF59==9) & (mc85_91_EF121==8 | mc85_91_EF121==9)
replace as=1 if (mc91_EF59==2 & mc85_91_EF121==9)
replace as=3 if (mc91_EF59==1 & (mc85_91_EF121==8 | mc85_91_EF121==9)) | (mc91_EF59==2 & mc85_91_EF121==8)

recode mc85_91_EF122 1=1 2=5 3=4 4 5=6 6 7=7 8 9=0, generate(ba)

generate is=99
replace is=1 if (as==0 & ba==1) | (as==1 & (ba==0 | ba==1))
replace is=3 if ((as==3 | as==4) & (ba==0 | ba==1 | ba==4))
replace is=4 if (as==5 & (ba==0 | ba==1 | ba==4))
replace is=5 if ((as==0 | as==1 | as==3 | as==4 | as==5) & ba==5)
replace is=6 if (as==6 & (ba==0 | ba==1 | ba==4))
replace is=7 if (as==6 & ba==5)
replace is=8 if (ba==6)
replace is=9 if (ba==7)

generate mc85_91_is = is if year == 1991
drop as ba is

**1989
recode mc85_91_EF121 1=4 2=5 3 4=6 8=2 9=0, generate(as)
recode mc85_91_EF122 1=1 2=5 3=4 4=6 5 6=7 8=2 9=0, generate(ba)

generate is=99
replace is=1 if (as==0 & ba==1) | (as==2 & (ba==0 | ba==1))
replace is=3 if ((as==0 | as==2) & ba==4) | (as==4 & (ba==0 | ba==1 | ba==2 | ba==4))
replace is=4 if (as==5 & (ba==0 | ba==1 | ba==2 | ba==4))
replace is=5 if ((as==0 | as==2 | as==4 | as==5) & ba==5)
replace is=6 if (as==6 & (ba==0 | ba==1 | ba==2 | ba==4))
replace is=7 if (as==6 & ba==5)
replace is=8 if (ba==6)
replace is=9 if (ba==7)

replace mc85_91_is = is if year == 1989
drop as ba is

**1985
recode mc85_91_EF121 1=4 2=5 3 4=6 0=2 9=0, generate(as)
recode mc85_91_EF122 1=1 2=5 3=4 4=6 5 6=7 0=2 9=0, generate(ba)

generate is=99
replace is=1 if (as==0 & ba==1) | (as==2 & (ba==0 | ba==1))
replace is=3 if ((as==0 | as==2) & ba==4) | (as==4 & (ba==0 | ba==1 | ba==2 | ba==4))
replace is=4 if (as==5 & (ba==0 | ba==1 | ba==2 | ba==4))
replace is=5 if ((as==0 | as==2 | as==4 | as==5) & ba==5)
replace is=6 if (as==6 & (ba==0 | ba==1 | ba==2 | ba==4))
replace is=7 if (as==6 & ba==5)
replace is=8 if (ba==6)
replace is=9 if (ba==7)

replace mc85_91_is = is if year == 1985
drop as ba is

*** b. Generate new variables that pool across years and label them

*survey design variables
generate psuid = mc05_EF3S if year==2005 //primary sampling unit
	replace psuid = mc00_EF3s if year==2000
	replace psuid = mc85_91_EF3S if year==1991
	replace psuid = mc85_91_EF3s if inlist(year,1985,1989)
	
generate hhid = mc05_EF4S if year==2005
	replace hhid = mc00_EF4s if year==2000
	replace hhid = mc85_91_EF4S if year==1991
	replace hhid = mc85_91_EF4s if inlist(year,1985,1989)
	
generate perid = mc05_EF5S if year==2005
	replace perid = mc00_EF5s if year==2000
	replace perid = mc85_91_EF5S if year==1991
	replace perid = mc85_91_EF5s if inlist(year,1985,1989)
	
generate weight = mc05_EF952 if year==2005 //extrapolation factor (combination of design weights and non-response correction)
	replace weight = mc00_EF750 if year==2000
	replace weight = mc85_91_EF253 if inlist(year,1985,1989,1991)
	
drop if weight == .
drop if weight == 0

* strata identifier is generated here
* first digit indicates the microcensus year (1=1985,2=1989,3=1991,4=2000,5=2005)
* digit 2 indicates the quarter (2005 MC only)
* digits 3 and 4 indicate the federal state
* digit 5 indicates the building layer
recode mc05_EF570 (6/7=6) // only 2005 has this last category
generate schicht = 50000 + mc05_EF12*1000 + mc05_EF1*10 + mc05_EF570 if year==2005
	replace schicht = 40000 + mc00_EF1*10 + mc00_EF712 if year==2000
	replace schicht = 30000 + mc85_91_EF1*10 + mc91_EF307 if year==1991
	replace schicht = 20000 + mc85_91_EF1*10 if year==1989
	replace schicht = 10000 + mc85_91_EF1*10 if year==1985

gen f = 0.01 //finite population correction factor	

*population at second residence -- to exclude	
generate not_primary_res = mc05_EF30 == 3 if year==2005
	replace not_primary_res = mc00_EF505 == 3 if year==2000
	replace not_primary_res = mc85_91_EF26 == 3 if inlist(year,1985,1989,1991)	
	
*female dummy
generate female = (mc05_EF46==2) if year==2005
	replace female = (mc00_EF32==2) if year==2000
	replace female = (mc85_91_EF35==2) if inlist(year,1985,1989,1991)
	label define female_value_label 1 "Female" 0 "Male"
	label values female female_value_label
		
**generate variables for analysis (outcomes and predictors)
*birth year
gen birth_year = mc05_EF47 if year == 2005
	replace birth_year = mc00_EF33 if year == 2000
	replace birth_year = mc85_91_EF37 if inlist(year,1985,1989,1991)
		replace birth_year = birth_year + 1900 if birth_year>=0 & birth_year<=91 & year==1991
		replace birth_year = birth_year + 1800 if birth_year>=98 & birth_year<=99 & year==1991
		replace birth_year = 1897 if birth_year==97 & year==1991
		replace birth_year = birth_year + 1900 if birth_year>=0 & birth_year<=89 & year==1989
		replace birth_year = birth_year + 1800 if birth_year>=96 & birth_year<=99 & year==1989
		replace birth_year = 1895 if birth_year==95 & year==1989
		replace birth_year = birth_year + 1900 if birth_year>=0 & birth_year<=85 & year==1985
		replace birth_year = birth_year + 1800 if birth_year>=92 & birth_year<=99 & year==1985
		replace birth_year = 1891 if birth_year==91 & year==1985

*age measure based on birth year (so will be fixed across survey years)
gen age_alt = year - birth_year		
		
** generate indicators for specific country or country groups. 
gen native = (mc05_EF366 == 1  | mc05_EF367 < 1955) if year == 2005 // born in current territory of FRG or immigrated before 1955 (same def as census)
	replace native = (mc00_EF43 == 1 | mc00_EF53 < 1955) if year == 2000 //german citizen or born in FRG or immigrated before 1955
	replace native = (mc85_91_EF41 == 1 | mc85_91_EF47 < 55) if inlist(year,1985,1989,1991) //german citizen or born in FRG or immigrated before 1955

gen poland = (mc05_EF369 == 23 | mc05_EF370 == 23 | (mc05_EF374 == 23 & mc05_EF373>=2000 & mc05_EF373!=.)) if year == 2005 //current or former Polish citizen
	replace poland = mc00_EF44 == 15 | mc00_EF45 == 15 if year == 2000
	replace poland = mc85_91_EF41 == 16 if inlist(year,1985,1989,1991)
gen czechoslovakia = (mc05_EF369 == 33 | mc05_EF369 == 30 | mc05_EF370 == 33 | mc05_EF370 == 30 | ((mc05_EF374 == 33 | mc05_EF374 == 30) & mc05_EF373>=2000 & mc05_EF373!=.)) if year == 2005
	replace czechoslovakia = mc00_EF44 == 21 | mc00_EF45 == 21 if year == 2000
	replace czechoslovakia = mc85_91_EF41 == 22 if year == 1991
	replace czechoslovakia = mc85_91_EF41 == 21 if inlist(year,1985,1989)
gen hungary = (mc05_EF369 == 35 | mc05_EF370 == 35 | (mc05_EF374 == 35 & mc05_EF373>=2000 & mc05_EF373!=.)) if year == 2005
	replace hungary = mc00_EF44 == 24 | mc00_EF45 == 24 if year == 2000
	replace hungary = mc85_91_EF41 == 25 if year == 1991
	replace hungary = mc85_91_EF41 == 24 if inlist(year,1985,1989)	
gen romania = (mc05_EF369 == 25 | mc05_EF370 == 25 | (mc05_EF374 == 25 & mc05_EF373>=2000 & mc05_EF373!=.)) if year == 2005
	replace romania = mc00_EF44 == 17 | mc00_EF45 == 17 if year == 2000
	replace romania = mc85_91_EF41 == 18 if year == 1991
	replace romania = 0 if inlist(year,1985,1989) //not observed these years, want summ command to run
gen bulgaria = mc05_EF369 == 3 | mc05_EF370 == 3 | (mc05_EF374 == 3 & mc05_EF373>=2000 & mc05_EF373!=.) if year == 2005
	replace bulgaria = mc85_91_EF41 == 4 if year == 1991
	replace bulgaria = 0 if inlist(year,1985,1989,2000) //not observed these years, want summ command to run	
gen USSR = inlist(mc05_EF369,5,26) | inlist(mc05_EF370,5,26) | (inlist(mc05_EF374,5,26) & mc05_EF373>=2000 & mc05_EF373!=.) if year == 2005
	replace USSR = mc00_EF44 == 8 | mc00_EF45 == 8 if year == 2000
	replace USSR = mc85_91_EF41 == 24 if year == 1991
	replace USSR = 0 if inlist(year,1985,1989) //not observed these years, want summ command to run
gen latvia = (mc05_EF369 == 15 | mc05_EF370 == 15 | (mc05_EF374 == 15 & mc05_EF373>=2000 & mc05_EF373!=.)) if year == 2005	
	replace latvia = 0 if inlist(year,1985,1989,1991,2000)
gen lithuania = (mc05_EF369 == 17 | mc05_EF370 == 17 | (mc05_EF374 == 17 & mc05_EF373>=2000 & mc05_EF373!=.)) if year == 2005	
	replace lithuania = 0 if inlist(year,1985,1989,1991,2000)
gen estonia = (mc05_EF369 == 7 | mc05_EF370 == 7 | (mc05_EF374 == 7 & mc05_EF373>=2000 & mc05_EF373!=.)) if year == 2005	
	replace estonia = 0 if inlist(year,1985,1989,1991,2000)
	
gen denmark = mc05_EF369 == 4 | mc05_EF370 == 4 | (mc05_EF374 == 4 & mc05_EF373>=2000 & mc05_EF373!=.) if year == 2005
	replace denmark = mc00_EF44 == 3 | mc00_EF45 == 3 if year == 2000
	replace denmark = mc85_91_EF41 == 5 if year == 1991
	replace denmark = mc85_91_EF41 == 4 if inlist(year,1985,1989)
gen finland = mc05_EF369 == 8 | mc05_EF370 == 8 | (mc05_EF374 == 8 & mc05_EF373>=2000 & mc05_EF373!=.) if year == 2005
	replace finland = mc00_EF44 == 4 | mc00_EF45 == 4 if year == 2000
	replace finland = 0 if year == 1991 //not listed
	replace finland = 0 if inlist(year,1985,1989) //not listed
gen norway = mc05_EF369 == 20 | mc05_EF370 == 20 | (mc05_EF374 == 20 & mc05_EF373>=2000 & mc05_EF373!=.) if year == 2005
	replace norway = 0 if year == 2000 //not listed
	replace norway = mc85_91_EF41 == 14 if year == 1991
	replace norway = mc85_91_EF41 == 14 if inlist(year,1985,1989)
gen sweden = mc05_EF369 == 27 | mc05_EF370 == 27 | (mc05_EF374 == 27 & mc05_EF373>=2000 & mc05_EF373!=.) if year == 2005
	replace sweden = mc00_EF44 == 18 | mc00_EF45 == 27 if year == 2000
	replace sweden = mc85_91_EF41 == 19 if year == 1991
	replace sweden = mc85_91_EF41 == 18 if inlist(year,1985,1989)
gen iceland = mc05_EF369 == 12 | mc05_EF370 == 12 | (mc05_EF374 == 12 & mc05_EF373>=2000 & mc05_EF373!=.) if year == 2005
	replace iceland = 0 if inlist(year,1985,1989,1991,2000)
gen liechtenstein = mc05_EF369 == 16 | mc05_EF370 == 16 | (mc05_EF374 == 16 & mc05_EF373>=2000 & mc05_EF373!=.) if year == 2005
	replace liechtenstein = 0 if inlist(year,1985,1989,1991,2000)
gen luxemburg = mc05_EF369 == 18 | mc05_EF370 == 18 | (mc05_EF374 == 18 & mc05_EF373>=2000 & mc05_EF373!=.) if year == 2005
	replace luxemburg = mc00_EF44 == 12 | mc00_EF45 == 12 if year == 2000
	replace luxemburg = mc85_91_EF41 == 11 if year == 1991
	replace luxemburg = mc85_91_EF41 == 11 if inlist(year,1985,1989)
gen britain = mc05_EF369 == 36 | mc05_EF370 == 36 | (mc05_EF374 == 36 & mc05_EF373>=2000 & mc05_EF373!=.) if year == 2005
	replace britain = mc00_EF44 == 7 | mc00_EF45 == 7 if year == 2000
	replace britain = mc85_91_EF41 == 8 if year == 1991
	replace britain = mc85_91_EF41 == 7 if inlist(year,1985,1989)
gen ireland = mc05_EF369 == 11 | mc05_EF370 == 11 | (mc05_EF374 == 11 & mc05_EF373>=2000 & mc05_EF373!=.) if year == 2005
	replace ireland = mc00_EF44 == 9 | mc00_EF45 == 11 if year == 2000
	replace ireland = mc85_91_EF41 == 9 if year == 1991
	replace ireland = mc85_91_EF41 == 8 if inlist(year,1985,1989)
gen belgium = mc05_EF369 == 1 | mc05_EF370 == 1 | (mc05_EF374 == 1 & mc05_EF373>=2000 & mc05_EF373!=.) if year == 2005
	replace belgium = mc00_EF44 == 1 | mc00_EF45 == 1 if year == 2000
	replace belgium = mc85_91_EF41 == 3 if year == 1991
	replace belgium = mc85_91_EF41 == 3 if inlist(year,1985,1989)
gen france = mc05_EF369 == 9 | mc05_EF370 == 9 | (mc05_EF374 == 9 & mc05_EF373>=2000 & mc05_EF373!=.) if year == 2005
	replace france = mc00_EF44 == 5 | mc00_EF45 == 5 if year == 2000
	replace france = mc85_91_EF41 == 6 if year == 1991
	replace france = mc85_91_EF41 == 5 if inlist(year,1985,1989)
gen netherlands = mc05_EF369 == 21 | mc05_EF370 == 21 | (mc05_EF374 == 21 & mc05_EF373>=2000 & mc05_EF373!=.) if year == 2005
	replace netherlands = mc00_EF44 == 13 | mc00_EF45 == 13 if year == 2000
	replace netherlands = mc85_91_EF41 == 13 if year == 1991
	replace netherlands = mc85_91_EF41 == 13 if inlist(year,1985,1989)
gen switzerland = mc05_EF369 == 28 | mc05_EF370 == 28 | (mc05_EF374 == 28 & mc05_EF373>=2000 & mc05_EF373!=.) if year == 2005
	replace switzerland = mc00_EF44 == 19 | mc00_EF45 == 19 if year == 2000
	replace switzerland = mc85_91_EF41 == 20 if year == 1991
	replace switzerland = mc85_91_EF41 == 19 if inlist(year,1985,1989)
gen greece = mc05_EF369 == 10 | mc05_EF370 == 10 | (mc05_EF374 == 10 & mc05_EF373>=2000 & mc05_EF373!=.) if year == 2005
	replace greece = mc00_EF44 == 6 | mc00_EF45 == 6 if year == 2000
	replace greece = mc85_91_EF41 == 7 if year == 1991
	replace greece = mc85_91_EF41 == 6 if inlist(year,1985,1989)
gen italy = mc05_EF369 == 13 | mc05_EF370 == 13 | (mc05_EF374 == 13 & mc05_EF373>=2000 & mc05_EF373!=.) if year == 2005
	replace italy = mc00_EF44 == 10 | mc00_EF45 == 10 if year == 2000
	replace italy = mc85_91_EF41 == 10 if year == 1991
	replace italy = mc85_91_EF41 == 9 if inlist(year,1985,1989)
gen portugal = mc05_EF369 == 24 | mc05_EF370 == 24 | (mc05_EF374 == 24 & mc05_EF373>=2000 & mc05_EF373!=.) if year == 2005
	replace portugal = mc00_EF44 == 16 | mc00_EF45 == 16 if year == 2000
	replace portugal = mc85_91_EF41 == 17 if year == 1991
	replace portugal = mc85_91_EF41 == 17 if inlist(year,1985,1989)
gen spain = mc05_EF369 == 32 | mc05_EF370 == 32 | (mc05_EF374 == 32 & mc05_EF373>=2000 & mc05_EF373!=.) if year == 2005
	replace spain = mc00_EF44 == 22 | mc00_EF45 == 22 if year == 2000
	replace spain = mc85_91_EF41 == 21 if year == 1991
	replace spain = mc85_91_EF41 == 20 if inlist(year,1985,1989)
gen malta = mc05_EF369 == 19 | mc05_EF370 == 19 | (mc05_EF374 == 19 & mc05_EF373>=2000 & mc05_EF373!=.) if year == 2005
	replace malta = 0 if inlist(year,1985,1989,1991,2000)	
gen cyprus = mc05_EF369 == 37 | mc05_EF370 == 37 | (mc05_EF374 == 37 & mc05_EF373>=2000 & mc05_EF373!=.) if year == 2005
	replace cyprus = 0 if inlist(year,1985,1989,1991,2000)
	
*exclude native-born from foreign country groupings
global foreign_countries poland czechoslovakia hungary romania bulgaria USSR latvia lithuania estonia denmark finland norway sweden iceland liechtenstein luxemburg britain ireland belgium france netherlands switzerland greece italy portugal spain malta cyprus
foreach c in $foreign_countries {
	replace `c' = 0 if native == 1
}
global countries native $foreign_countries
	
*year of first immigration to FRG
generate immigration_year = mc05_EF367 if year==2005
replace immigration_year = mc00_EF53 if year==2000
	recode immigration_year (1900 9999 =.) if year==2000 //code for german born or missing
replace immigration_year = mc85_91_EF47 if inlist(year,1985,1989,1991)
	recode immigration_year (0 99 9999 = .)	//code for german born or missing
	replace immigration_year = 1900+immigration_year if inlist(year,1985,1989,1991)
	
*aggregate regional groups
gen Sat = max(poland,czechoslovakia,hungary,romania,bulgaria) * (immigration_year >= 1955 & immigration_year != .)
gen Sov = max(USSR,latvia,lithuania,estonia) * (immigration_year >= 1955 & immigration_year != .)
gen EB = max(Sat,Sov)
gen West = max(denmark,finland,norway,sweden,iceland,liechtenstein,luxemburg,britain,ireland,belgium,france,netherlands,switzerland,greece,italy,portugal,spain,malta,cyprus) * (immigration_year >= 1955 & immigration_year != .)
gen Imm = max(West,EB)
	
*other demographic variables
gen immigration_age = age_alt - (year - immigration_year) // age at immigration
	replace immigration_age = 0 if immigration_age < 0
	replace immigration_age = 900 if native == 1

gen years_in_germany = year - immigration_year
	replace years_in_germany = 900 if native == 1	
	
** main time periods
gen post_87 = immigration_year>=1987 & immigration_year != .
gen post_93 = immigration_year>=1993 & immigration_year != .
gen post_62 = immigration_year>=1962 & immigration_year != .

gen btw_80_84 = immigration_year>=1980 & immigration_year<=1984
gen btw_87_90 = immigration_year>=1987 & immigration_year<=1990
gen btw_95_99 = immigration_year>=1995 & immigration_year<=1999
gen btw_87_92 = immigration_year>=1987 & immigration_year<=1992

*** Interaction Terms
local stub_names "EB Imm West"
foreach stub in `stub_names'{
	
	* Origin * Arrival year
	foreach period in post_87 post_93 post_62 btw_80_84 btw_87_90 btw_95_99 btw_87_92{
		gen `stub'_`period' = `period' * `stub'
	}	
	
	*interactions with survey years for assimilation tests
	gen `stub'_btw_80_84_y85 = `stub'_btw_80_84 * year == 1985 
	gen `stub'_btw_80_84_y89 = `stub'_btw_80_84 * year == 1989 
	gen `stub'_btw_80_84_y91 = `stub'_btw_80_84 * year == 1991 
	gen `stub'_btw_80_84_y00 = `stub'_btw_80_84 * year == 2000 
	gen `stub'_btw_80_84_y05 = `stub'_btw_80_84 * year == 2005 

	gen `stub'_btw_87_90_y91 = `stub'_btw_87_90 * year == 1991 
	gen `stub'_btw_87_90_y00 = `stub'_btw_87_90 * year == 2000 
	gen `stub'_btw_87_90_y05 = `stub'_btw_87_90 * year == 2005 

	gen `stub'_btw_95_99_y00 = `stub'_btw_95_99 * year == 2000 
	gen `stub'_btw_95_99_y05 = `stub'_btw_95_99 * year == 2005
}

* Outcome: years of education
gen isced97 = mc05_is if year==2005
	replace isced97 = mc00_is if year==2000
	replace isced97 = mc85_91_is if inlist(year,1985,1989,1991)
	
gen edu_years = 0
	replace edu_years = 10 if isced97 == 3 //level 2B
	replace edu_years = 11 if isced97 == 4 //level 2A
	replace edu_years = 12 if isced97 == 5 //level 3B
	replace edu_years = 13 if isced97 == 6 //level 3A
	replace edu_years = 16 if isced97 == 7 //level 4A
	replace edu_years = 16 if isced97 == 8 //level 5B
	replace edu_years = 17 if isced97 == 9 //level 5A
	replace edu_years = 20 if isced97 == 10 //level 6
	
gen isced97_5a = isced97 >= 9 & isced97 != .
gen isced97_5b = isced97 >= 8 & isced97 != .
gen isced97_4 = isced97 >= 7 & isced97 != .
gen isced97_3a = isced97 >= 6 & isced97 != .
gen isced97_3b = isced97 >= 5 & isced97 != .
	
** Outcome: employment (question changes slightly over surveys)
gen employed = (mc05_EF84 == 1) if year == 2005
	replace employed = (mc00_EF95 == 1) if year == 2000
	replace employed = (mc85_91_EF61 == 1) if inlist(year,1985,1989,1991)

** Outcome: occupation (high medium low ISCO)
*map Klassifizierung der Berufe (1975 and 1992) to high/med/low classification used for census
gen job_code_92 = mc05_EF119 if year == 2005
	replace job_code_92 = mc00_EF128 if year == 2000
		replace job_code_92 = floor(job_code_92/100) //convert 4-digit to 2-digit
gen job_code_75 = mc85_91_EF93 if inlist(year,1985,1989,1991)
		replace job_code_75 = floor(job_code_75/100) //convert 4-digit to 2-digit

gen high_occ = inlist(job_code_92,76,67,60,61,69,70,72,75,77,81,82,83,84,87,88,89,90,30,62,63,64,65,73,85,86) if inlist(year,2000,2005) //ISCO 1,2,3
	replace high_occ = inlist(job_code_75,76,67,60,61,69,70,72,75,77,81,82,83,84,87,88,89,90,30,62,63,64,65,73,85,86) if inlist(year,1985,1989,1991)
		
gen med_occ = inlist(job_code_92,3,6,10,11,12,13,14,15,16,17,18,19,20,21,26,27,28,29,30,31,39,40,41,42,43,44,45,46,47,48,49,50,51,22,23,24,25,32,33,34,35,36,37,52,53,54,55,71,78,66,67,68,80,91) if inlist(year,2000,2005) //ISCO 4,5,6,7,8
	replace med_occ = inlist(job_code_75,3,6,10,11,12,13,14,15,16,17,18,19,20,21,26,27,28,29,30,31,39,40,41,42,43,44,45,46,47,48,49,50,51,22,23,24,25,32,33,34,35,36,37,52,53,54,55,71,78,68,80,91) if inlist(year,1985,1989,1991)

replace high_occ = 0 if employed == 0
replace med_occ = 0 if employed == 0

gen high_med_occ = max(high_occ,med_occ) //high or medium


** Outcome: net monthly income
gen income = mc05_EF436 if year == 2005
		recode income (1 = 75) (2  = 225) (3 = 400) (4 = 600) (5 = 800) (6 = 1000) (7 = 1200) (8 = 1400) (9 = 1600) (10 = 1850) (11 = 2150) (12 = 2450) (13 = 2750) (14 = 3050) (15 = 3400) (16 = 3800) (17 = 4250) (18 = 4750) (19 = 5250) (20 = 5750) (21 = 6750) (22 = 8750) (23 = 14000) (24 = 18000) (50 90 99 . = 0) if year == 2005
	replace income = mc00_EF372 if year == 2000
		recode income (1 = 150) (2  = 450) (3 = 800) (4 = 1200) (5 = 1600) (6 = 2000) (7 = 2350) (8 = 2750) (9 = 3250) (10 = 3750) (11 = 4250) (12 = 4750) (13 = 5250) (14 = 5750) (15 = 6250) (16 = 6750) (17 = 7250) (18 = 7750) (19 = 9000) (20 = 11000) (21 = 13500) (22 = 17500) (23 = 27500) (24 = 35000) (50 90 99 . = 0) if year == 2000
	replace income = mc85_91_EF146 if year == 1991
		recode income (1 = 150) (2  = 450) (3 = 800) (4 = 1200) (5 = 1600) (6 = 2000) (7 = 2350) (8 = 2750) (9 = 3250) (10 = 3750) (11 = 4250) (12 = 4750) (13 = 5250) (14 = 5750) (15 = 6250) (16 = 6750) (17 = 7250) (18 = 7500) (50 99 . = 0) if year == 1991	
	replace income = mc85_91_EF146 if inlist(year,1985,1989)
		recode income (1 = 150) (2  = 375) (3 = 525) (4 = 700) (5 = 900) (6 = 1100) (7 = 1300) (8 = 1500) (9 = 1700) (10 = 1900) (11 = 2100) (12 = 2350) (13 = 2750) (14 = 3250) (15 = 3750) (16 = 4250) (17 = 4750) (18 = 5000) (50 99 . = 0) if inlist(year,1985,1989)

*convert to contemporaneous USD from https://fxtop.com/en/historical-exchange-rates.php
replace income = income * 1.244114 if year == 2005
	replace income = income * 0.472235 if year == 2000
	replace income = income * 0.604093 if year == 1991
	replace income = income * 0.532581 if year == 1989
	replace income = income * 0.342807 if year == 1985
	
*convert to 1999 USD to match US census using https://www.usinflationcalculator.com/
replace income = income * 0.85 if year == 2005
	replace income = income * 0.97 if year == 2000
	replace income = income * 1.22 if year == 1991
	replace income = income * 1.34 if year == 1989
	replace income = income * 1.55 if year == 1985
	
*transform with log
gen log_income = log(income)

*has family at origin
gen spouse_origin = mc05_EF375 == 1 if year == 2005
	replace spouse_origin = mc00_EF54 == 1 if year == 2000
	replace spouse_origin = mc85_91_EF48 == 1 if inlist(year,1991,1989,1985)

gen kids_origin = 0
	replace kids_origin = kids_origin + mc05_EF377 if mc05_EF377 <= 20 & year == 2005
	replace kids_origin = kids_origin + mc05_EF378 if mc05_EF378 <= 20 & year == 2005
	replace kids_origin = kids_origin + mc05_EF379 if mc05_EF379 <= 20 & year == 2005
	replace kids_origin = kids_origin + mc00_EF56 if mc00_EF56 <= 8 & year == 2000
	replace kids_origin = kids_origin + mc00_EF57 if mc00_EF57 <= 8 & year == 2000
	replace kids_origin = kids_origin + mc00_EF58 if mc00_EF58 <= 8 & year == 2000
	replace kids_origin = kids_origin + mc85_91_EF50 if mc85_91_EF50 <= 8 & inlist(year,1985,1989,1991)
	replace kids_origin = kids_origin + mc85_91_EF51 if mc85_91_EF51 <= 8 & inlist(year,1985,1989,1991)
	replace kids_origin = kids_origin + mc85_91_EF52 if mc85_91_EF52 <= 8 & inlist(year,1985,1989,1991)
	replace kids_origin = kids_origin + mc85_91_EF53 if mc85_91_EF53 <= 8 & inlist(year,1985,1989,1991)

*convert to binary
replace kids_origin = (kids_origin > 0 & kids_origin != .)

gen family_origin = max(spouse_origin,kids_origin)	
drop spouse_origin kids_origin

*bins for control vars
gen age_bin = floor(age_alt/5)*5 //5-yr bins
gen years_in_germany_bin = floor(years_in_germany/5)*5 //5-yr bins
	replace years_in_germany_bin = years_in_germany if years_in_germany<=5 //1-yr bins for first 5 years


********************************************************************************************************************
*** II. Data analysis
********************************************************************************************************************

* Only weighted results are needed.

drop if not_primary_res == 1 //population not at main residence - not part of sampling frame

*generate indicator for sample of interest for main analysis
gen sample = (age_alt >= 25 & age_alt <= 65)
	replace sample = 0 if native == 0 & Imm == 0 // include only natives, W. Europeans, and CB migrants
	replace sample = 0 if Imm == 1 & immigration_age <=24 //exclude those arriving before age 25
	replace sample = 0 if Imm == 1 & (immigration_year > 2003) // exclude those who came after 2003, when many CB countries join EU
	replace sample = 0 if Imm == 1 & (immigration_year < 1962) // exclude those who came before 1962 (first post-berlin wall year)
	replace sample = 0 if Imm == 1 & (immigration_year == . | immigration_age == .) //can't use these in analysis
	
*generate indicator for sample of interest for Berlin wall analysis
gen sample_wall = (age_alt >= 25)
	replace sample_wall = 0 if native == 0 & Imm == 0 // include only natives, W. Europeans, and CB migrants
	replace sample_wall = 0 if Imm == 1 & immigration_age <=24 //exclude those arriving before age 25
	replace sample_wall = 0 if Imm == 1 & (immigration_year > 1986) // pre-period only
	replace sample_wall = 0 if Imm == 1 & (immigration_year < 1955) // exclude those who came before 1955 (census def of immigrant)
	replace sample_wall = 0 if Imm == 1 & (immigration_year == . | immigration_age == .) //can't use these in analysis
	replace sample_wall = . if not_primary_res == 1 //population not at main residence - not part of sampling frame
	
*generate indicator for sample of interest for Berlin wall analysis, with no age restriction
gen sample_wall_allages = 1
	replace sample_wall_allages = 0 if native == 0 & Imm == 0 // include only natives, W. Europeans, and CB migrants
	replace sample_wall_allages = 0 if Imm == 1 & (immigration_year > 1986) // pre-period only
	replace sample_wall_allages = 0 if Imm == 1 & (immigration_year < 1955) // exclude those who came before 1955 (census def of immigrant)
	replace sample_wall_allages = 0 if Imm == 1 & (immigration_year == . | immigration_age == .) //can't use these in analysis
	replace sample_wall_allages = . if not_primary_res == 1 //population not at main residence - not part of sampling frame		
	
*generate pooled weights
gen weight_pooled = weight
count
local tot_obs = r(N)
levelsof year, local(years)
    foreach y of local years {
	count if year == `y'
	display "year `y' has r(N) obs"
	replace weight_pooled = weight * r(N) / `tot_obs' if year == `y'
	}	
	
*global with universal controls
global controls_sel i.age_bin##female
global controls_out i.age_bin##female i.years_in_germany_bin##female

********************************************************************************************************************
*** a. Weighted regression analyses ***

svyset psuid [pw = weight_pooled], strata(schicht) fpc(f) singleunit(scaled)

*** Fig B5 -- Trajectories
local append_replace replace
local iter = 0
foreach outcome in employed high_occ high_med_occ log_income {
	
local iter = `iter' + 1

if `iter' == 2{
local append_replace append
}

capture noisily svy, subpop(sample): reg `outcome' EB_btw_95_99_y05 EB_btw_95_99_y00 EB_btw_87_90_y05 EB_btw_87_90_y00 EB_btw_87_90_y91 EB_btw_80_84_y05 EB_btw_80_84_y00 EB_btw_80_84_y91 EB_btw_80_84_y89 EB_btw_80_84_y85 Imm_btw_95_99_y05 Imm_btw_95_99_y00 Imm_btw_87_90_y05 Imm_btw_87_90_y00 Imm_btw_87_90_y91 Imm_btw_80_84_y05 Imm_btw_80_84_y00 Imm_btw_80_84_y91 Imm_btw_80_84_y89 Imm_btw_80_84_y85 ${countries} i.year $controls_out
	
capture noisily outreg2 using "$outputpath\figb5.xls", `append_replace' label stats(coef se) nonotes sortvar(EB_btw_95_99_y05 EB_btw_95_99_y00 EB_btw_87_90_y05 EB_btw_87_90_y00 EB_btw_87_90_y91 EB_btw_80_84_y05 EB_btw_80_84_y00 EB_btw_80_84_y91 EB_btw_80_84_y89 EB_btw_80_84_y85) keep(EB_btw_95_99_y05 EB_btw_95_99_y00 EB_btw_87_90_y05 EB_btw_87_90_y00 EB_btw_87_90_y91 EB_btw_80_84_y05 EB_btw_80_84_y00 EB_btw_80_84_y91 EB_btw_80_84_y89 EB_btw_80_84_y85)

}
capture noisily erase "$outputpath\figb5.txt"

*** Table B3 -- Mincer Returns
gen exper = age_alt - edu_years - 6
gen exper2 = exper*exper

local append_replace replace
foreach outcome in employed high_occ high_med_occ log_income {
	capture noisily svy, subpop(sample if EB == 1): reg `outcome' edu_years exper exper2 female i.year ${countries}
	capture noisily outreg2 using "$outputpath\tb3.xls", `append_replace' label stats(coef se) nonotes dec(3) sortvar(edu_years) keep(edu_years) noaster
	local append_replace append
}
capture noisily erase "$outputpath\tb3.txt"
drop exper exper2


*** Table B8 -- Berlin Wall + Main Estimates
capture noisily svy, subpop(sample_wall_allages): reg edu_years EB_post_62 Imm_post_62 ${countries} i.year $controls_sel
capture noisily outreg2 using "$outputpath\tb8.xls", replace label stats(coef se) nonotes dec(3) afmt(g) addstat("Subpop Obs", `e(N_sub)') sortvar(EB_post_62 Imm_post_62) keep(EB_post_62 Imm_post_62)

capture noisily svy, subpop(sample_wall): reg edu_years EB_post_62 Imm_post_62 ${countries} i.year $controls_sel
capture noisily outreg2 using "$outputpath\tb8.xls", append label stats(coef se) nonotes dec(3) afmt(g) addstat("Subpop Obs", `e(N_sub)') sortvar(EB_post_62 Imm_post_62) keep(EB_post_62 Imm_post_62)

capture noisily svy, subpop(sample if max(native,EB)==1): reg edu_years EB_post_93 EB_post_87 ${countries} i.year $controls_sel
capture noisily outreg2 using "$outputpath\tb8.xls", append label stats(coef se) nonotes dec(3) afmt(g) addstat("Subpop Obs", `e(N_sub)') sortvar(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87) keep(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87)

capture noisily svy, subpop(sample): reg edu_years EB_post_93 EB_post_87 Imm_post_93 Imm_post_87 ${countries} i.year $controls_sel
capture noisily outreg2 using "$outputpath\tb8.xls", append label stats(coef se) nonotes dec(3) afmt(g) addstat("Subpop Obs", `e(N_sub)') sortvar(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87) keep(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87)

capture noisily erase "$outputpath\tb8.txt"


*** Table D2 -- Family Reunification
gen years_in_germany_lateref = years_in_germany * btw_87_92
gen years_in_germany_econ = years_in_germany * post_93

gen EB_years_in_germany_lateref = years_in_germany * EB_btw_87_92
gen EB_years_in_germany_econ = years_in_germany * EB_post_93
gen EB_years_in_germany = years_in_germany * EB


capture noisily svy: mean family_origin if sample == 1 & EB == 1
capture noisily local depvar_mean_eb = e(b)[1,1]

capture noisily svy, subpop(sample if EB == 1): reg family_origin years_in_germany years_in_germany_lateref years_in_germany_econ i.year
capture noisily outreg2 using "$outputpath\td2.xls", replace label stats(coef se) nonotes dec(3) addstat("Subpop Obs", `e(N_sub)', "depvar_mean_eb", `depvar_mean_eb') sortvar(years_in_germany years_in_germany_lateref years_in_germany_econ) keep(years_in_germany years_in_germany_lateref years_in_germany_econ)
capture noisily svy, subpop(sample if EB == 1): reg family_origin years_in_germany years_in_germany_lateref years_in_germany_econ i.year $controls_sel ${countries}
capture noisily outreg2 using "$outputpath\td2.xls", append label stats(coef se) nonotes dec(3) addstat("Subpop Obs", `e(N_sub)', "depvar_mean_eb", `depvar_mean_eb') sortvar(years_in_germany years_in_germany_lateref years_in_germany_econ) keep(years_in_germany years_in_germany_lateref years_in_germany_econ)

capture noisily svy, subpop(sample if Imm == 1): reg family_origin EB_years_in_germany EB_years_in_germany_lateref EB_years_in_germany_econ years_in_germany years_in_germany_lateref years_in_germany_econ i.year
capture noisily outreg2 using "$outputpath\td2.xls", append label stats(coef se) nonotes dec(3) addstat("Subpop Obs", `e(N_sub)', "depvar_mean_eb", `depvar_mean_eb') sortvar(EB_years_in_germany EB_years_in_germany_lateref EB_years_in_germany_econ years_in_germany years_in_germany_lateref years_in_germany_econ) keep(EB_years_in_germany EB_years_in_germany_lateref EB_years_in_germany_econ years_in_germany years_in_germany_lateref years_in_germany_econ)
capture noisily svy, subpop(sample if Imm == 1): reg family_origin EB_years_in_germany EB_years_in_germany_lateref EB_years_in_germany_econ years_in_germany years_in_germany_lateref years_in_germany_econ i.year $controls_sel ${countries}
capture noisily outreg2 using "$outputpath\td2.xls", append label stats(coef se) nonotes dec(3) addstat("Subpop Obs", `e(N_sub)', "depvar_mean_eb", `depvar_mean_eb') sortvar(EB_years_in_germany EB_years_in_germany_lateref EB_years_in_germany_econ years_in_germany years_in_germany_lateref years_in_germany_econ) keep(EB_years_in_germany EB_years_in_germany_lateref EB_years_in_germany_econ years_in_germany years_in_germany_lateref years_in_germany_econ)

*descriptive
capture noisily svy, subpop(sample if EB == 1): reg family_origin years_in_germany if btw_87_92 == 0 & post_93 == 0
capture noisily svy, subpop(sample if EB == 1): reg family_origin years_in_germany if btw_87_92 == 1
capture noisily svy, subpop(sample if EB == 1): reg family_origin years_in_germany if post_93 == 1

drop years_in_germany_lateref years_in_germany_econ EB_years_in_germany_lateref EB_years_in_germany_econ EB_years_in_germany

capture noisily erase "$outputpath\td2.txt"


*** Fig D2 -- Differential attrition
capture noisily svy, subpop(sample): reg age_alt EB_btw_95_99_y05 EB_btw_95_99_y00 EB_btw_87_90_y05 EB_btw_87_90_y00 EB_btw_87_90_y91 EB_btw_80_84_y05 EB_btw_80_84_y00 EB_btw_80_84_y91 EB_btw_80_84_y89 EB_btw_80_84_y85 Imm_btw_95_99_y05 Imm_btw_95_99_y00 Imm_btw_87_90_y05 Imm_btw_87_90_y00 Imm_btw_87_90_y91 Imm_btw_80_84_y05 Imm_btw_80_84_y00 Imm_btw_80_84_y91 Imm_btw_80_84_y89 Imm_btw_80_84_y85 ${countries} i.year $controls_sel	
capture noisily outreg2 using "$outputpath\figd2.xls", replace label stats(coef se) nonotes sortvar(EB_btw_95_99_y05 EB_btw_95_99_y00 EB_btw_87_90_y05 EB_btw_87_90_y00 EB_btw_87_90_y91 EB_btw_80_84_y05 EB_btw_80_84_y00 EB_btw_80_84_y91 EB_btw_80_84_y89 EB_btw_80_84_y85) keep(EB_btw_95_99_y05 EB_btw_95_99_y00 EB_btw_87_90_y05 EB_btw_87_90_y00 EB_btw_87_90_y91 EB_btw_80_84_y05 EB_btw_80_84_y00 EB_btw_80_84_y91 EB_btw_80_84_y89 EB_btw_80_84_y85)

capture noisily svy, subpop(sample): reg female EB_btw_95_99_y05 EB_btw_95_99_y00 EB_btw_87_90_y05 EB_btw_87_90_y00 EB_btw_87_90_y91 EB_btw_80_84_y05 EB_btw_80_84_y00 EB_btw_80_84_y91 EB_btw_80_84_y89 EB_btw_80_84_y85 Imm_btw_95_99_y05 Imm_btw_95_99_y00 Imm_btw_87_90_y05 Imm_btw_87_90_y00 Imm_btw_87_90_y91 Imm_btw_80_84_y05 Imm_btw_80_84_y00 Imm_btw_80_84_y91 Imm_btw_80_84_y89 Imm_btw_80_84_y85 ${countries} i.year i.age_bin	
capture noisily outreg2 using "$outputpath\figd2.xls", append label stats(coef se) nonotes sortvar(EB_btw_95_99_y05 EB_btw_95_99_y00 EB_btw_87_90_y05 EB_btw_87_90_y00 EB_btw_87_90_y91 EB_btw_80_84_y05 EB_btw_80_84_y00 EB_btw_80_84_y91 EB_btw_80_84_y89 EB_btw_80_84_y85) keep(EB_btw_95_99_y05 EB_btw_95_99_y00 EB_btw_87_90_y05 EB_btw_87_90_y00 EB_btw_87_90_y91 EB_btw_80_84_y05 EB_btw_80_84_y00 EB_btw_80_84_y91 EB_btw_80_84_y89 EB_btw_80_84_y85)

capture noisily svy, subpop(sample if Imm == 1): reg immigration_year EB_btw_95_99_y05 EB_btw_95_99_y00 EB_btw_87_90_y05 EB_btw_87_90_y00 EB_btw_87_90_y91 EB_btw_80_84_y05 EB_btw_80_84_y00 EB_btw_80_84_y91 EB_btw_80_84_y89 EB_btw_80_84_y85 Imm_btw_95_99_y05 Imm_btw_95_99_y00 Imm_btw_87_90_y05 Imm_btw_87_90_y00 Imm_btw_87_90_y91 Imm_btw_80_84_y05 Imm_btw_80_84_y00 Imm_btw_80_84_y91 Imm_btw_80_84_y89 Imm_btw_80_84_y85 ${countries} i.year $controls_sel
capture noisily outreg2 using "$outputpath\figd2.xls", append label stats(coef se) nonotes sortvar(EB_btw_95_99_y05 EB_btw_95_99_y00 EB_btw_87_90_y05 EB_btw_87_90_y00 EB_btw_87_90_y91 EB_btw_80_84_y05 EB_btw_80_84_y00 EB_btw_80_84_y91 EB_btw_80_84_y89 EB_btw_80_84_y85) keep(EB_btw_95_99_y05 EB_btw_95_99_y00 EB_btw_87_90_y05 EB_btw_87_90_y00 EB_btw_87_90_y91 EB_btw_80_84_y05 EB_btw_80_84_y00 EB_btw_80_84_y91 EB_btw_80_84_y89 EB_btw_80_84_y85)

capture noisily svy, subpop(sample): reg edu_years EB_btw_95_99_y05 EB_btw_95_99_y00 EB_btw_87_90_y05 EB_btw_87_90_y00 EB_btw_87_90_y91 EB_btw_80_84_y05 EB_btw_80_84_y00 EB_btw_80_84_y91 EB_btw_80_84_y89 EB_btw_80_84_y85 Imm_btw_95_99_y05 Imm_btw_95_99_y00 Imm_btw_87_90_y05 Imm_btw_87_90_y00 Imm_btw_87_90_y91 Imm_btw_80_84_y05 Imm_btw_80_84_y00 Imm_btw_80_84_y91 Imm_btw_80_84_y89 Imm_btw_80_84_y85 ${countries} i.year $controls_sel	
capture noisily outreg2 using "$outputpath\figd2.xls", append label stats(coef se) nonotes sortvar(EB_btw_95_99_y05 EB_btw_95_99_y00 EB_btw_87_90_y05 EB_btw_87_90_y00 EB_btw_87_90_y91 EB_btw_80_84_y05 EB_btw_80_84_y00 EB_btw_80_84_y91 EB_btw_80_84_y89 EB_btw_80_84_y85) keep(EB_btw_95_99_y05 EB_btw_95_99_y00 EB_btw_87_90_y05 EB_btw_87_90_y00 EB_btw_87_90_y91 EB_btw_80_84_y05 EB_btw_80_84_y00 EB_btw_80_84_y91 EB_btw_80_84_y89 EB_btw_80_84_y85)

capture noisily erase "$outputpath\figd2.txt"

